import Taro from '@tarojs/taro';
import {cozeApiRequest} from '../utils/aepApiRequest';
import { cozeUploadRequst } from '@/utils/uploadRequest';



export async function getAiList({ id }) {
  const user_id = Taro.getStorageSync('uid')
  const path = `v3/users/${user_id}/devices/${id}/agent`
  return cozeApiRequest(path, {
    method: 'GET',
  });
}
  

export async function getAiDevice({ user_id }) {
  const path = `v3/users/${user_id}/devices`
  return cozeApiRequest(path, {
    method: 'GET',
  });
}

export async function bindAiDevice(data) {
  const user_id = Taro.getStorageSync('uid')
  const path = `v3/users/${user_id}/devices`
  return cozeApiRequest(path, {
    method: 'POST',
    body: data
  });
}


export async function getDeviceAiList({ id }) {
  const path = `v3/devices/${id}/agents`
  return cozeApiRequest(path, {
    method: 'GET',
  });
}


export async function getAiDeviceVersion(params: {
  id: string;
}) {
  const { id } = params;
  // const datas = {
  //   success: true,
  //   data: {
  //     hard_version: '1.0.0',
  //     soft_version: '1.1.0',
  //   }
  // }
  // return datas;
  const user_id = Taro.getStorageSync('uid')

  const data = await cozeApiRequest(`v3/users/${user_id}/devices/${id}`, {
    method: 'GET',
    body: {},
  });
  console.log('datadatadata', data)
  if (data.success) {
    return {
      success: true,
      data: {
        soft_version: data.data?.data?.firmware?.comm?.sw_version,
        hard_version: data.data?.data?.firmware?.comm?.hw_version,
      },
    }
  }
  return { success: false, data: {} }
}


export async function getAiUseProductList({ user_id,device_id }) {
  const path = `v3/users/${user_id}/devices/${device_id}/agent`
  return cozeApiRequest(path, {
    method: 'GET',
  });
}

export async function getRecordText({ user_id }) {
  const path = `v3/users/${user_id}/voices/prompt`
  return cozeApiRequest(path, {
    method: 'GET',
  });
}

export async function getAuthState() {
  const user_id = Taro.getStorageSync('uid')
  const url = `v3/users/${user_id}/coze/auths`
  return cozeApiRequest(url, {
    method: 'GET',
  });
}

export async function deleteAuth() {
  const user_id = Taro.getStorageSync('uid')
  const url = `v3/users/${user_id}/coze/auths`
  return cozeApiRequest(url, {
    method: 'DELETE',
  });
}

export async function createAuth() {
  const user_id = Taro.getStorageSync('uid')
  const url = `v3/users/${user_id}/coze/auths`
  return cozeApiRequest(url, {
    method: 'POST',
  });
}

export async function getAuthDevices() {
  const user_id = Taro.getStorageSync('uid')
  const url = `v3/users/${user_id}/coze/devices`
  return cozeApiRequest(url, {
    method: 'GET',
  });
}

export async function setAuthDevices({dids}: {dids: string[]}) {
  const user_id = Taro.getStorageSync('uid')
  const url = `v3/users/${user_id}/coze/devices`
  return cozeApiRequest(url, {
    method: 'PATCH',
    body: {
      device_ids: dids
    }
  });
}

export async function updateAiUseProductList({ user_id, device_id ,agent_id,voice_id}) {
  const path = `v3/users/${user_id}/devices/${device_id}/agent`
  return cozeApiRequest(path, {
    method: 'PUT',
    body: {
      agent_id,
      voice_id
    }
  });
}

  export async function getAiSelfVoiceList({ user_id }) {
    const path = `v3/users/${user_id}/voices`
    return cozeApiRequest(path, {
      method: 'GET',
    });
  }


  export async function getSystemVoiceList({ product_key, agent_id }) {
    const path = `v3/products/${product_key}/agents/${agent_id}/voices`
    return cozeApiRequest(path, {
      method: 'GET',
    });
  }

  export async function newCopyVoice({ user_id, voice_id, path, name, content }) {
    const url = `v3/users/${user_id}/voices/clone`
    console.log('newCopyVoicenewCopyVoice', url, path, voice_id, name, content)
    return cozeUploadRequst(url, { filePath: path, name: 'audio_file', formData: {
      voice_id,
      name,
      desc:name,
      language: 'zh',
      audio_text: content,
      audio_format: 'mp3'
    } });
  }
  
  export async function deleteDeviceContext(deviceId: string) {
    const path = `v3/devices/${deviceId}/context`;
    return cozeApiRequest(path, {
      method: 'DELETE',
    });
  }

// 创建用户自定义智能体
export async function createUserAgent({ name, desc, prompt, prologue, voice_id, icon_file }) {
  const user_id = Taro.getStorageSync('uid');
  const url = `v3/users/${user_id}/agents`;
  
  return cozeUploadRequst(url, { 
    filePath: icon_file, 
    name: 'icon_file', 
    formData: {
      name,
      desc,
      prompt,
      prologue,
      voice_id
    } 
  });
}

// 更新用户自定义智能体
export async function updateUserAgent(agent_id, { name, desc, prompt, prologue, voice_id, icon_file }) {
  const user_id = Taro.getStorageSync('uid');
  const url = `v3/users/${user_id}/agents/${agent_id}`;
  
  // 如果有新图标文件，使用上传请求
  if (icon_file) {
    return cozeUploadRequst(url, { 
      filePath: icon_file, 
      name: 'icon_file', 
      formData: {
        name,
        desc,
        prompt,
        prologue,
        voice_id
      },
      method: 'PUT'
    });
  } 
  
  // 没有新图标文件，使用普通请求
  return cozeApiRequest(url, {
    method: 'PUT',
    body: {
      name,
      desc,
      prompt,
      prologue,
      voice_id
    }
  });
}

// 删除用户自定义智能体
export async function deleteUserAgent(agent_id) {
  const user_id = Taro.getStorageSync('uid');
  const url = `v3/users/${user_id}/agents/${agent_id}`;
  
  return cozeApiRequest(url, {
    method: 'DELETE'
  });
}

export async function getUserAgentList({ user_id }) {
  const url = `v3/users/${user_id}/agents`;
  
  return cozeApiRequest(url, {
    method: 'GET'
  });
}
// 获取用户自定义智能体详情
export async function getUserAgent(agent_id) {
  const user_id = Taro.getStorageSync('uid');
  const url = `v3/users/${user_id}/agents/${agent_id}`;
  
  return cozeApiRequest(url, {
    method: 'GET'
  });
}

// 获取用户系统音色
export async function getSystemVoices({ platform_type, language }: { platform_type?: string, language?: string }) {
  const user_id = Taro.getStorageSync('uid');
  let url = `v3/users/${user_id}/system-voices`;
  
  // 添加查询参数
  const params: any = [];
  if (platform_type !== undefined) params.push(`platform_type=${platform_type}`);
  if (language) params.push(`language=${language}`);
  
  if (params.length > 0) {
    url += `?${params.join('&')}`;
  }
  
  return cozeApiRequest(url, {
    method: 'GET'
  });
}

// 获取设备与智能体的消息记录
export async function getDeviceMessages(params: {
  device_id: string;
  agent_id: string;
  cursor?: string;
  limit?: string;
}) {
  const { device_id, agent_id, cursor, limit } = params;
  const user_id = Taro.getStorageSync('uid');
  let url = `v3/users/${user_id}/devices/${device_id}/messages?agent_id=${agent_id}`;
  
  if (cursor) {
    url += `&cursor=${cursor}`;
  }
  if (limit) {
    url += `&limit=${limit}`;
  }

  return cozeApiRequest(url, {
    method: 'GET',
  });
}

// 查看设备可升级固件
export async function getDeviceOtaTask(deviceId: string) {
  const user_id = Taro.getStorageSync('uid');
  const url = `v3/users/${user_id}/devices/${deviceId}/ota-tasks`;
  
  return cozeApiRequest(url, {
    method: 'GET'
  });
}

// 推送设备可升级固件
export async function pushDeviceOtaTask(deviceId: string, taskId: string) {
  const user_id = Taro.getStorageSync('uid');
  const url = `v3/users/${user_id}/devices/${deviceId}/ota-tasks/${taskId}`;
  
  return cozeApiRequest(url, {
    method: 'PATCH'
  });
}

// 确认设备可升级固件
export async function confirmDeviceOtaTask(deviceId: string, taskId: string) {
  const user_id = Taro.getStorageSync('uid');
  const url = `v3/users/${user_id}/devices/${deviceId}/ota-tasks/${taskId}`;
  
  return cozeApiRequest(url, {
    method: 'PUT'
  });
}